﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemBUS;
using NangLuongTieuDiemDTO.DTO;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucTuVanSanPham : UserControl
    {

        DataTable dt_pinnangluong;
        DataTable dt_chonpinnangluong;
        DataTable dt_bodieukhienxac;
        DataTable dt_chonbodieukhienxac;

        double tongcongsuat = ucTinhCongSuatThietBiDien.tongcongsuat;
      

        public ucTuVanSanPham()
        {
            InitializeComponent();
        }

     
        private void ucTuVanThietBiDien_Load(object sender, EventArgs e)
        {
            try
            {
                double watpin = tongcongsuat / 4;
                txt_watpin.Text = watpin.ToString();
                txt_tongcongsuat.Text = tongcongsuat.ToString(); ;
                List<SanPhamDTO> dspinnangluong = new List<SanPhamDTO>();
                dspinnangluong = NangLuongTieuDiem_Management.TimKiemTuVanPinNangLuong(watpin, 1, 2, 0);


                // add thong tin cot vao datatable

                dt_pinnangluong = new DataTable();
                dt_chonpinnangluong = new DataTable();

                // add data source vao grid control
                dgv_pinnangluong.DataSource = dt_pinnangluong;
                dgv_chonpinnangluong.DataSource = dt_chonpinnangluong;

                // datatable pinnangluong
                dt_pinnangluong.Columns.Add(new DataColumn("stt"));
                dt_pinnangluong.Columns.Add(new DataColumn("Tensanpham"));
                dt_pinnangluong.Columns.Add(new DataColumn("Loaisanpham"));
                dt_pinnangluong.Columns.Add(new DataColumn("Congsuat"));
                dt_pinnangluong.Columns.Add(new DataColumn("Dai"));
                dt_pinnangluong.Columns.Add(new DataColumn("Rong"));
                dt_pinnangluong.Columns.Add(new DataColumn("Day"));
                dt_pinnangluong.Columns.Add(new DataColumn("Isc"));
                dt_pinnangluong.Columns.Add(new DataColumn("Giaban"));

                // datatable chonpinnangluong
                dt_chonpinnangluong.Columns.Add(new DataColumn("stt"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Tensanpham"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Loaisanpham"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Congsuat"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Dai"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Rong"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Day"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Isc"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Giaban"));
                dt_chonpinnangluong.Columns.Add(new DataColumn("Soluong"));


                int stt = 1;

                for (int i = 0; i < dspinnangluong.Count; i++)
                {
                    DataRow dtrow = dt_pinnangluong.NewRow();
                    dtrow["stt"] = stt;
                    dtrow["Tensanpham"] = dspinnangluong[i].TenSanPham;
                    dtrow["Loaisanpham"] = dspinnangluong[i].LoaiSanPhamID;
                    dtrow["Congsuat"] = dspinnangluong[i].CongSuat;
                    dtrow["Dai"] = dspinnangluong[i].Dai;
                    dtrow["Rong"] = dspinnangluong[i].Rong;
                    dtrow["Day"] = dspinnangluong[i].Day;
                    dtrow["Isc"] = dspinnangluong[i].Isc;
                    dtrow["Giaban"] = dspinnangluong[i].Gia;
                    dt_pinnangluong.Rows.Add(dtrow);
                    stt++;
                }

                // danh sach loaisanpham lay tu database 
                List<LoaiSanPhamDTO> dsloaisanpham = new List<LoaiSanPhamDTO>();
                dsloaisanpham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll();


                // hien thi ten loai sanpham tren grid pinnangluong
                repositoryItemGrid_LookUpEdit_pinnangluong.DataSource = dsloaisanpham;
                repositoryItemGrid_LookUpEdit_pinnangluong.ValueMember = "ID";
                repositoryItemGrid_LookUpEdit_pinnangluong.DisplayMember = "TenLoaiSanPham";

                loaisanpham.ColumnEdit = repositoryItemGrid_LookUpEdit_pinnangluong;

                // hien thi ten loai sanpham tren grid chonpinnangluong
                repositoryItemGridLookUpEdit_chonpinnangluong.DataSource = dsloaisanpham;
                repositoryItemGridLookUpEdit_chonpinnangluong.ValueMember = "ID";
                repositoryItemGridLookUpEdit_chonpinnangluong.DisplayMember = "TenLoaiSanPham";
         
                loaisanpham_chonpin.ColumnEdit = repositoryItemGridLookUpEdit_chonpinnangluong;
               

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("ucTuVanThietBiDien_Load", ex);
                throw;
            }
         
        }

       
          

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion

   

      

        private void txt_themcongsuat_EditValueChanged_1(object sender, EventArgs e)
        {
            double congsuat;
            if (txt_themcongsuat.Text != "")
            {
                txt_tongcongsuat.Text = (tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text)).ToString();
                if (txt_songayduphong.Text != "")
                {
                    congsuat = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                    congsuat = congsuat / Convert.ToInt32(txt_songayduphong.Text);
                }
                else
                {
                    congsuat = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                }

            }

            else
            {
                txt_tongcongsuat.Text = tongcongsuat.ToString();
                if (txt_songayduphong.Text != "")
                {
                    congsuat = tongcongsuat;
                    congsuat = congsuat / Convert.ToInt32(txt_songayduphong.Text);
                }
                else
                {
                    congsuat = tongcongsuat;
                }
            }


            double watpin = congsuat / 4;
            txt_watpin.Text = watpin.ToString();

            List<SanPhamDTO> dspinnangluong = new List<SanPhamDTO>();
            dspinnangluong = NangLuongTieuDiem_Management.TimKiemTuVanPinNangLuong(watpin, 1, 2, 0);

            // clear data trong datatable pinnangluong
          
                dt_pinnangluong.Rows.Clear();
            

            // clear data trong datatable chonpinnangluon      
                dt_chonpinnangluong.Rows.Clear();
            

            // set lai data cho grid pinnangluong
            int stt = 1;
            for (int i = 0; i < dspinnangluong.Count; i++)
            {
                DataRow dtrow = dt_pinnangluong.NewRow();
                dtrow["stt"] = stt;
                dtrow["Tensanpham"] = dspinnangluong[i].TenSanPham;
                dtrow["Loaisanpham"] = dspinnangluong[i].LoaiSanPhamID;
                dtrow["Congsuat"] = dspinnangluong[i].CongSuat;
                dtrow["Dai"] = dspinnangluong[i].Dai;
                dtrow["Rong"] = dspinnangluong[i].Rong;
                dtrow["Day"] = dspinnangluong[i].Day;
                dtrow["Isc"] = dspinnangluong[i].Isc;
                dtrow["Giaban"] = dspinnangluong[i].Gia;
                dt_pinnangluong.Rows.Add(dtrow);
                stt++;
            }
           
        }

        private void rad_dunghangnhay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rad_dunghangnhay.Checked == true)
                {
                    txt_songayduphong.Enabled = false;

                    if (txt_songayduphong.Text != "")
                    {
                        txt_songayduphong.Text = string.Empty;
                    }

                    double tongcongsuat_new;
                    if (txt_themcongsuat.Text != "")
                    {
                        tongcongsuat_new = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                    }
                    else
                    {
                        tongcongsuat_new = tongcongsuat;
                    }

                    double watpin = tongcongsuat_new / 4;
                    txt_watpin.Text = watpin.ToString();




                    // load grid view pin nang luong

                    txt_watpin.Text = watpin.ToString();

                    List<SanPhamDTO> dspinnangluong = new List<SanPhamDTO>();
                    dspinnangluong = NangLuongTieuDiem_Management.TimKiemTuVanPinNangLuong(watpin, 1, 2, 0);

                    int stt = 1;


                    dt_pinnangluong.Rows.Clear();

                    // set data cho grid pinnangluong
                    for (int i = 0; i < dspinnangluong.Count; i++)
                    {
                        DataRow dtrow = dt_pinnangluong.NewRow();
                        dtrow["stt"] = stt;
                        dtrow["Tensanpham"] = dspinnangluong[i].TenSanPham;
                        dtrow["Loaisanpham"] = dspinnangluong[i].LoaiSanPhamID;
                        dtrow["Congsuat"] = dspinnangluong[i].CongSuat;
                        dtrow["Dai"] = dspinnangluong[i].Dai;
                        dtrow["Rong"] = dspinnangluong[i].Rong;
                        dtrow["Day"] = dspinnangluong[i].Day;
                        dtrow["Isc"] = dspinnangluong[i].Isc;
                        dtrow["Giaban"] = dspinnangluong[i].Gia;
                        dt_pinnangluong.Rows.Add(dtrow);
                        stt++;
                    }

                    // clear data trong datatable chonpinnangluong

                    dt_chonpinnangluong.Rows.Clear();


                }

                if (rad_duphongcupdien.Checked == true)
                {
                    txt_songayduphong.Enabled = true;



                    double tongcongsuat_new;
                    if (txt_themcongsuat.Text != "")
                    {
                        tongcongsuat_new = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                    }
                    else
                    {
                        tongcongsuat_new = tongcongsuat;
                    }

                    double watpin = tongcongsuat_new / 4;
                    txt_watpin.Text = watpin.ToString();


                    // clear data trong datatable chonpinnangluong

                    dt_chonpinnangluong.Rows.Clear();

                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("rad_dunghangnhay_CheckedChanged", ex);

            }
        }

        private void txt_songayduphong_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                double tongcongsuat_new;
                double watpin;
                if (txt_songayduphong.Text != "")
                {

                    if (txt_themcongsuat.Text != "")
                    {
                        tongcongsuat_new = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                    }
                    else
                    {
                        tongcongsuat_new = tongcongsuat;
                    }

                    double congsuat = tongcongsuat_new / Convert.ToInt32(txt_songayduphong.Text);
                    watpin = congsuat / 4;
                    txt_watpin.Text = watpin.ToString();

                }
                else
                {
                    if (txt_themcongsuat.Text != "")
                    {
                        tongcongsuat_new = tongcongsuat + Convert.ToDouble(txt_themcongsuat.Text);
                    }
                    else
                    {
                        tongcongsuat_new = tongcongsuat;
                    }

                    double congsuat = tongcongsuat_new;
                    watpin = congsuat / 4;
                    txt_watpin.Text = watpin.ToString();
                }

                // load grid view pin nang luong


                List<SanPhamDTO> dspinnangluong = new List<SanPhamDTO>();
                dspinnangluong = NangLuongTieuDiem_Management.TimKiemTuVanPinNangLuong(watpin, 1, 2, 0);

                int stt = 1;
                //dt_pinnangluong.Reset();

                // clear data trong datatable pinnangluong
                for (int i = 0; i < dt_pinnangluong.Rows.Count; i++)
                {
                    dt_pinnangluong.Rows.Clear();
                }

                // clear data trong datatable chonpinnangluong
                for (int i = 0; i < dt_chonpinnangluong.Rows.Count; i++)
                {
                    dt_chonpinnangluong.Rows.Clear();
                }

                // set lai data cho grid pinnangluong
                for (int i = 0; i < dspinnangluong.Count; i++)
                {
                    DataRow dtrow = dt_pinnangluong.NewRow();
                    dtrow["stt"] = stt;
                    dtrow["Tensanpham"] = dspinnangluong[i].TenSanPham;
                    dtrow["Loaisanpham"] = dspinnangluong[i].LoaiSanPhamID;
                    dtrow["Congsuat"] = dspinnangluong[i].CongSuat;
                    dtrow["Dai"] = dspinnangluong[i].Dai;
                    dtrow["Rong"] = dspinnangluong[i].Rong;
                    dtrow["Day"] = dspinnangluong[i].Day;
                    dtrow["Isc"] = dspinnangluong[i].Isc;
                    dtrow["Giaban"] = dspinnangluong[i].Gia;
                    dt_pinnangluong.Rows.Add(dtrow);
                    stt++;
                }




            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("txt_songayduphong_EditValueChanged", ex);
                throw;
            }
        }

        private void btn_pin_next_Click_2(object sender, EventArgs e)
        {
            try
            {
                if (gridView_pinnangluong.GetFocusedDataRow() == null)
                {               
                    return;
                }

                DataRow drow_pin = dt_pinnangluong.NewRow();
                DataRow drow_chonpin = dt_chonpinnangluong.NewRow();

                drow_pin = gridView_pinnangluong.GetFocusedDataRow();

                drow_chonpin["Tensanpham"] = drow_pin["Tensanpham"];
                drow_chonpin["Loaisanpham"] = drow_pin["Loaisanpham"];
                drow_chonpin["Congsuat"] = drow_pin["Congsuat"];
                drow_chonpin["Dai"] = drow_pin["Dai"];
                drow_chonpin["Rong"] = drow_pin["Rong"];
                drow_chonpin["Day"] = drow_pin["Day"];
                drow_chonpin["Isc"] = drow_pin["Isc"];
                drow_chonpin["Giaban"] = drow_pin["Giaban"];

                dt_chonpinnangluong.Rows.Add(drow_chonpin);

                dt_pinnangluong.Rows.Remove(drow_pin);

                // set lai stt cho grid pinnangluong
                int stt = 1;
                for (int i = 0; i < dt_pinnangluong.Rows.Count; i++)
                {
                    gridView_pinnangluong.SetRowCellValue(i, "stt", stt);
                    stt++;
                }

                // set lai stt cho grid chonpinnangluong
                stt = 1;
                for (int i = 0; i < dt_chonpinnangluong.Rows.Count; i++)
                {

                    gridview_chonpinnangluong.SetRowCellValue(i, "stt", stt);
                    stt++;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btn_previous_Click_2", ex);
                throw;
            }
            
       
        }

        private void btn_previous_Click_2(object sender, EventArgs e)
        {
            try
            {
                if (gridview_chonpinnangluong.GetFocusedDataRow() == null)
                {              
                    return;
                }
                DataRow drow_pin = dt_pinnangluong.NewRow();
                DataRow drow_chonpin = dt_chonpinnangluong.NewRow();

                drow_chonpin = gridview_chonpinnangluong.GetFocusedDataRow();

                drow_pin["Tensanpham"] = drow_chonpin["Tensanpham"];
                drow_pin["Loaisanpham"] = drow_chonpin["Loaisanpham"];
                drow_pin["Congsuat"] = drow_chonpin["Congsuat"];
                drow_pin["Dai"] = drow_chonpin["Dai"];
                drow_pin["Rong"] = drow_chonpin["Rong"];
                drow_pin["Day"] = drow_chonpin["Day"];
                drow_pin["Isc"] = drow_chonpin["Isc"];
                drow_pin["Giaban"] = drow_chonpin["Giaban"];

                dt_pinnangluong.Rows.Add(drow_pin);

                dt_chonpinnangluong.Rows.Remove(drow_chonpin);

                // set lai stt cho grid pinnangluong
                int stt = 1;
                for (int i = 0; i < dt_pinnangluong.Rows.Count; i++)
                {
                    gridView_pinnangluong.SetRowCellValue(i, "stt", stt);
                    stt++;
                }

                // set lai stt cho grid chonpinnangluong
                stt = 1;
                for (int i = 0; i < dt_chonpinnangluong.Rows.Count; i++)
                {

                    gridview_chonpinnangluong.SetRowCellValue(i, "stt", stt);
                    stt++;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btn_previous_Click_2", ex);
                throw;
            }
            
        }

        private void txt_themcongsuat_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                    e.Handled = true;

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("txt_themcongsuat_KeyPress_1", ex);
            }
        }

        private void txt_songayduphong_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                    e.Handled = true;

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("txt_songayduphong_KeyPress_1", ex);
            }
        }

        private void btn_chondieukhienxac_Click(object sender, EventArgs e)
        {
            try
            {
                // kiem tra chon pin nang luong
                if (dt_chonpinnangluong.Rows.Count == 0)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Chưa chọn pin năng lượng  ", "Thông báo", true);
                    return;
                }

                // kiem tra nhap so luong
                for (int i = 0; i < dt_chonpinnangluong.Rows.Count; i++)
                {
                    if (dt_chonpinnangluong.Rows[i]["Soluong"].ToString() == "")
                    {
                        ClassUtils.Utils.MassageBoxErrorOtInformation("Chưa nhập vào số lượng  ", "Thông báo", true);
                        return;
                    }
                }

                // set hien thi group chodieukhienxac
                group_chondieukhienxac.Visible = true;

                // tim pin nang luong co cuong do dong dien sinh ra lon nhat
                double Isc_max = Convert.ToDouble(dt_chonpinnangluong.Rows[0]["Isc"]);
                for (int i = 0; i <  dt_chonpinnangluong.Rows.Count; i++)
                {
                    if (Convert.ToDouble(dt_chonpinnangluong.Rows[i]["Isc"]) > Isc_max)
                    {
                        Isc_max = Convert.ToDouble(dt_chonpinnangluong.Rows[i]["Isc"]);
                    }
                }

                // tim kiem bo dieu khien xac co I(A) >= Isc_max

                // add thong tin cot vao datatable

                dt_bodieukhienxac= new DataTable();
                dt_chonbodieukhienxac = new DataTable();

                // datatable bodieukhienxac
                dt_bodieukhienxac.Columns.Add(new DataColumn("stt"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Tenbodieukhien"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Loaisanpham"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Vmp"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Voc"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Imp"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Gia"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Nguonnhap"));
                dt_bodieukhienxac.Columns.Add(new DataColumn("Ghichu"));

                List<SanPhamDTO> dsbodieukhien = new List<SanPhamDTO>();
                dsbodieukhien = NangLuongTieuDiem_Management.TimKiemTuVanBoDieuKhienXac(Isc_max);

                int stt = 1;

                for (int i = 0; i < dsbodieukhien.Count; i++)
                {
                    DataRow dtrow = dt_bodieukhienxac.NewRow();
                    dtrow["stt"] = stt;
                    dtrow["Tenbodieukhien"] = dsbodieukhien[i].TenSanPham;
                    dtrow["Loaisanpham"] = dsbodieukhien[i].LoaiSanPhamID;
                    dtrow["Voc"] = dsbodieukhien[i].Voc;
                    dtrow["Vmp"] = dsbodieukhien[i].Vmp;
                    dtrow["Imp"] = dsbodieukhien[i].Imp;
                    dtrow["Gia"] = dsbodieukhien[i].Gia;
                    dtrow["Nguonnhap"] = dsbodieukhien[i].NguonNhap;
                    dtrow["Ghichu"] = dsbodieukhien[i].GhiChu;
                    dt_bodieukhienxac.Rows.Add(dtrow);
                    stt++;
                }

                dgv_bodieukhien.DataSource = dt_bodieukhienxac;

                // datatable chonbodieukhienxac
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("stt"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Tenbodieukhien"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Loaisanpham"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Vmp"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Voc"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Imp"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Gia"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Nguonnhap"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Ghichu"));
                dt_chonbodieukhienxac.Columns.Add(new DataColumn("Soluong"));

                dgv_chobodieukhien.DataSource = dt_chonbodieukhienxac;

                // danh sach loaisanpham lay tu database 
                List<LoaiSanPhamDTO> dsloaisanpham = new List<LoaiSanPhamDTO>();
                dsloaisanpham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll();


                // hien thi ten loai sanpham tren grid pinnangluong
              

                repositoryItemGridLookUpEdit_bodieukhien.DataSource = dsloaisanpham;
                repositoryItemGridLookUpEdit_bodieukhien.ValueMember = "ID";
                repositoryItemGridLookUpEdit_bodieukhien.DisplayMember = "TenLoaiSanPham";


                Loaisanpham_bodieukhienxac.ColumnEdit = repositoryItemGridLookUpEdit_bodieukhien;

                // hien thi ten loai sanpham tren grid chonpinnangluong
              

                repositoryItemGridLookUpEdit_chonbodieukhien.DataSource = dsloaisanpham;
                repositoryItemGridLookUpEdit_chonbodieukhien.ValueMember = "ID";
                repositoryItemGridLookUpEdit_chonbodieukhien.DisplayMember = "TenLoaiSanPham";

                Loaisanpham_chonbodieukhien.ColumnEdit = repositoryItemGridLookUpEdit_chonbodieukhien;

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btn_chondieukhienxac_Click", ex);
            }
        }

       

        private void next_dieukhienxac_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridview_bodieukhien.GetFocusedDataRow() == null)
                {              
                    return;
                }

                DataRow drow_bodieukhienxac = dt_bodieukhienxac.NewRow() ;
                DataRow drow_chonbodieukhienxac = dt_chonbodieukhienxac.NewRow();

                drow_bodieukhienxac = gridview_bodieukhien.GetFocusedDataRow();

                drow_chonbodieukhienxac["Tenbodieukhien"] = drow_bodieukhienxac["Tenbodieukhien"];
                drow_chonbodieukhienxac["Loaisanpham"] = drow_bodieukhienxac["Loaisanpham"];
                drow_chonbodieukhienxac["Voc"] = drow_bodieukhienxac["Voc"];
                drow_chonbodieukhienxac["Vmp"] = drow_bodieukhienxac["Vmp"];
                drow_chonbodieukhienxac["Imp"] = drow_bodieukhienxac["Imp"];
                drow_chonbodieukhienxac["Gia"] = drow_bodieukhienxac["Gia"];
                drow_chonbodieukhienxac["Nguonnhap"] = drow_bodieukhienxac["Nguonnhap"];
                drow_chonbodieukhienxac["Ghichu"] = drow_bodieukhienxac["Ghichu"];

              

                dt_chonbodieukhienxac.Rows.Add(drow_chonbodieukhienxac);
                dt_bodieukhienxac.Rows.Remove(drow_bodieukhienxac);
              

                // set lai stt cho grid bodieukhienxac
                int stt = 1;
                for (int i = 0; i < dt_bodieukhienxac.Rows.Count; i++)
                {
                    gridview_bodieukhien.SetRowCellValue(i, "stt", stt);
                    stt++;
                }

                // set lai stt cho grid chonbodieukhienxac
                stt = 1;
                for (int i = 0; i < dt_chonbodieukhienxac.Rows.Count; i++)
                {

                    gridview_chondieukhienxac.SetRowCellValue(i, "stt", stt);
                    stt++;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("next_dieukhienxac_Click", ex);
              
            }
            
       
        }

        private void pre_dieukhienxac_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridview_chondieukhienxac.GetFocusedDataRow() == null)
                {
                    return;
                }

                DataRow drow_bodieukhienxac = dt_bodieukhienxac.NewRow();
                DataRow drow_chonbodieukhienxac = dt_chonbodieukhienxac.NewRow();

                drow_chonbodieukhienxac = gridview_chondieukhienxac.GetFocusedDataRow();

                drow_bodieukhienxac["Tenbodieukhien"] = drow_chonbodieukhienxac["Tenbodieukhien"];
                drow_bodieukhienxac["Loaisanpham"] = drow_chonbodieukhienxac["Loaisanpham"];
                drow_bodieukhienxac["Voc"] = drow_chonbodieukhienxac["Voc"];
                drow_bodieukhienxac["Vmp"] = drow_chonbodieukhienxac["Vmp"];
                drow_bodieukhienxac["Imp"] = drow_chonbodieukhienxac["Imp"];
                drow_bodieukhienxac["Gia"] = drow_chonbodieukhienxac["Gia"];
                drow_bodieukhienxac["Nguonnhap"] = drow_chonbodieukhienxac["Nguonnhap"];
                drow_bodieukhienxac["Ghichu"] = drow_chonbodieukhienxac["Ghichu"];



                dt_bodieukhienxac.Rows.Add(drow_bodieukhienxac);
                dt_chonbodieukhienxac.Rows.Remove(drow_chonbodieukhienxac);


                // set lai stt cho grid bodieukhienxac
                int stt = 1;
                for (int i = 0; i < dt_bodieukhienxac.Rows.Count; i++)
                {
                    gridview_bodieukhien.SetRowCellValue(i, "stt", stt);
                    stt++;
                }

                // set lai stt cho grid chonbodieukhienxac
                stt = 1;
                for (int i = 0; i < dt_chonbodieukhienxac.Rows.Count; i++)
                {

                    gridview_chondieukhienxac.SetRowCellValue(i, "stt", stt);
                    stt++;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("pre_dieukhienxac_Click", ex);
               
            }
            
        }

     

        
    }
}
